<?php
class sales_mdl_sales extends ome_mdl_sales{
    function __construct($app){
        parent::__construct(app::get('ome'));
    }
    
    public function table_name($real=false){
        $table_name = "sales";
        if($real){
            return kernel::database()->prefix.$this->app->app_id.'_'.$table_name;
        }else{
            return $table_name;
        }
    }
 function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(

            'sale_bn'=>app::get('base')->_('销售单号'),
            'iostock_bn'=>app::get('base')->_('出入库单号'),
            'original_bn'=>app::get('base')->_('发货单号'),
            'branch_bn'=>app::get('base')->_('仓库编号'),
        );
        return $Options = array_merge($parentOptions,$childOptions);
     }
     
     function _filter($filter,$tableAlias=null,$baseWhere=null){
       $where = '1';
       //仓库编号
        if(isset($filter['branch_bn'])){
           
            $branch = &app::get('ome')->model('branch');
            $branch_id = $branch->getList('branch_id',array('branch_bn'=>$filter['branch_bn']));
            $sql2 = 'SELECT sale_id FROM sdb_ome_sales WHERE branch_id like \''.$branch_id[0]['branch_id'].'\'';
            $rows = kernel::database()->select($sql2);
            $saleId[] = 0;
            foreach($rows as $row){
                $saleId[] = $row['sale_id'];
            }
            
            $where .= '  AND sale_id IN ('. implode(',', $saleId).')';
            unset($filter['branch_bn']);
           
        }
       //货号查询
       if(isset($filter['bn'])){
          
            $sql = 'SELECT sale_id FROM sdb_ome_sales_items WHERE bn like \''.$filter['bn'].'\'';
            $rows = kernel::database()->select($sql);
            $saleId[] = 0;
            foreach($rows as $row){
                $saleId[] = $row['sale_id'];
            }
            
            $where .= '  AND sale_id IN ('. implode(',', $saleId).')';
            unset($filter['bn']);
           
        }
       //货品名称
        if(isset($filter['product_name'])){
            $sql = 'SELECT bn FROM sdb_ome_products WHERE name like \''.$filter['product_name'].'\'';
            $name = kernel::database()->select($sql);
            $sql2 = 'SELECT sale_id FROM sdb_ome_sales_items WHERE bn like \''.$name[0]['bn'].'\'';
            $rows = kernel::database()->select($sql2); 
            $saleId[] = 0;
            foreach($rows as $row){
                $saleId[] = $row['sale_id'];
            }
            
            $where .= '  AND sale_id IN ('. implode(',', $saleId).')';
            unset($filter['name']);
           
        }
        //发货单号
       if(isset($filter['original_bn'])){
            $sql = 'SELECT iostock_id FROM sdb_ome_iostock WHERE original_bn like \''.$filter['original_bn'].'\'';
            $bn = kernel::database()->select($sql);
            $sql2 = 'SELECT sale_id FROM sdb_ome_sales_items WHERE iostock_id like \''.$bn[0]['iostock_id'].'\'';
            $rows = kernel::database()->select($sql2);
            $saleId[] = 0;
            foreach($rows as $row){
                $saleId[] = $row['sale_id'];
            }
            
            $where .= '  AND sale_id IN ('. implode(',', $saleId).')';
            unset($filter['original_bn']);
           
        }
         //订单号

        if(isset($filter['order_bn'])){
            $sql = 'SELECT order_id FROM sdb_ome_orders WHERE order_bn like \''.$filter['order_bn'].'\'';
            $branch_bn = kernel::database()->select($sql);
            $delivery = &app::get('ome')->model('delivery_order');
            $rows = $delivery->getList('delivery_id',array('order_id'=>$branch_bn[0]['order_id']));
           
            foreach($rows as $row){
               $sql2 = 'SELECT delivery_bn FROM sdb_ome_delivery WHERE delivery_id like \''.$row['delivery_id'].'\'';
               $original_bn = kernel::database()->select($sql2);
               $sql3 = 'SELECT iostock_id FROM sdb_ome_iostock WHERE original_bn like \''.$original_bn[0]['delivery_bn'].'\'';
               $iostock_id = kernel::database()->select($sql3);
               $sql4 = 'SELECT sale_id FROM sdb_ome_sales_items WHERE iostock_id like \''.$iostock_id[0]['iostock_id'].'\'';
               $sales = kernel::database()->select($sql4);
               $saleId[] = 0;
               foreach($sales as $sale){
                  $saleId[] = $sale['sale_id'];
               }
            }
            
            $where .= '  AND sale_id IN ('. implode(',', $saleId).')';
            unset($filter['branch_bn']);
           
        }
       //仓库名称
        if(isset($filter['branch_name'])){
           
            $branch = &app::get('ome')->model('branch');
            $branch_bn = $branch->getList('branch_id',array('name'=>$filter['branch_name']));
            $sql2 = 'SELECT sale_id FROM sdb_ome_sales WHERE branch_id like \''.$filter['branch_name'].'\'';
            $rows = kernel::database()->select($sql2);
            $saleId[] = 0;
            foreach($rows as $row){
                $saleId[] = $row['sale_id'];
            }
            
            $where .= '  AND sale_id IN ('. implode(',', $saleId).')';
            unset($filter['branch_bn']);
           
        }
        //销售类型
         if(isset($filter['type_id'])){
            $sql = 'SELECT iostock_id FROM sdb_ome_iostock WHERE type_id like \''.$filter['type_id'].'\'';
            $iostock_id = kernel::database()->select($sql);
            
            foreach($iostock_id as $id){
               
                $sql2 = 'SELECT sale_id FROM sdb_ome_sales_items WHERE iostock_id like \''.$id['iostock_id'].'\'';
                $rows = kernel::database()->select($sql2);
                $saleId[] = 0;
                    foreach($rows as $row){
                        $saleId[] = $row['sale_id'];
                    }
                
              
               }
            $where .= '  AND sale_id IN ('. implode(',', $saleId).')';
            unset($filter['type_id']);
            
        }
         //商店名称
        if(isset($filter['name'])){
            
            $sql2 = 'SELECT sale_id FROM sdb_ome_sales WHERE shop_id like \''.$filter['name'].'\'';
            $rows = kernel::database()->select($sql2);
            $saleId[] = 0;
            foreach($rows as $row){
                $saleId[] = $row['sale_id'];
            }
            
            $where .= '  AND sale_id IN ('. implode(',', $saleId).')';
            unset($filter['branch_bn']);
           
        }
       return parent::_filter($filter,$tableAlias,$baseWhere)." AND ".$where;
     }
	function exportTemplate($filter){
        foreach ($this->io_title($filter) as $v){
            $title[] = kernel::single('base_charset')->utf2local($v);
        }
        return $title;
    }
  
     function io_title( $filter=null,$ioType='csv' ){
			switch( $ioType ){
            case 'csv':
            default:
                $this->oSchema['csv']['sales'] = array(
                    '*:销售单号'       => '',
                    '*:店铺编号'       => '',
                    '*:店铺名称'       => '',
                    '*:发货单号'       => '',
                    '*:出入库单号'       => '',
                    '*:用户名'         => '',
                    '*:仓库编号'   => '',
                    '*:仓库名称'       => '',   
                    '*:销售金额'       => '',
                    '*:销售数量'       => '',
                    '*:预收物流费'         => '',
               		'*:预估物流费'         => '',
                    '*:附加费'         => '',
                    '*:订单折扣金额'   => '',
                    '*:预存款'         => '',
                    '*:备注'           => '',
                    '*:支付状态'       => '',
                    '*:销售时间'       => '',
	                '*:导购'       => '',
	                '*:物流公司'       => '',
	                '*:物流单号'       => '',
                );
                $this->oSchema['csv']['sales_items'] = array(
                    '*:销售单号'       => '',
                    '*:订单号'   => '',
                    '*:货号'       => '',
	                '*:货品名称'   => '',
	                '*:销售单价'   => '',
                    '*:销售数量'   => '',
                    '*:销售金额'   => '',
                	'*:导购'       => '',
                	'*:店铺编号'       => '',
                    '*:店铺名称'       => '',
                	'*:仓库编号'   => '',
                    '*:仓库名称'       => '', 
	                '*:用户名'         => '',  
	                '*:销售时间'       => '',
                );
                break;
        }
        $this->ioTitle[$ioType]['sales'] = array_keys( $this->oSchema[$ioType]['sales'] );
        $this->ioTitle[$ioType]['sales_items'] = array_keys( $this->oSchema[$ioType]['sales_items'] );
        return $this->ioTitle[$ioType][$filter];
     
	 }
     //csv导出
     function fgetlist_csv( &$data,$filter,$offset,$exportType = 1 ){
       //print_r($data);//$filter是选中sales表中的记录id
	   //$data是sales数据表
	   //$offset 是偏移值
	   // 
         if( !$data['title']['sales'] ){
             $title = array();
             foreach( $this->io_title('sales') as $k => $v ){
                 $title[] = $this->charset->utf2local($v);
                //$title[] = $v;
			 }
             $data['title']['sales'] = '"'.implode('","',$title).'"';
         }
         if( !$data['title']['sales_items'] ){
             $title = array();
              foreach( $this->io_title('sales_items') as $k => $v ){
                  $title[] = $this->charset->utf2local($v);
                  //$title[] = $v;
			 }
			 $data['title']['sales_items'] = '"'.implode('","',$title).'"';
         }
       
         $limit = 100;
         //if( $filter[''] )获取的sales的id
		 //$list=$this->getList('id',$filter,$offset*$limit,$limit);获取的sales的列表
	    
        $this->sales = &app::get('ome')->model('sales');
	    if(!$list = $this->sales->getList('sale_id',$filter,$offset*$limit,$limit)){
            return false;
        }
   
		 foreach( $list as $aFilter ){
                $aOrder = $this->sales->dump($aFilter['sale_id']);//获得sales表中的数据	
		        //提出sales 表中的数据，传给模板
                $shop = &app::get('ome')->model('shop');
                $shopid = $shop->getList('name',array('shop_id'=>$aOrder['shop_id']));
                $shopbn = $shop->getList('shop_bn',array('shop_id'=>$aOrder['shop_id']));
                $original = &app::get('ome')->model('iostock');
                $original_bn = $original->getList('original_bn',array('iostock_bn'=>$aOrder['iostock_bn']));
                $member = &app::get('ome')->model('members');
                $member_uname = $member->getList('uname',array('member_id'=>$aOrder['member_id']));
                $branch = &app::get('ome')->model('branch');
                $branch_bn = $branch->getList('branch_bn',array('branch_id'=>$aOrder['branch_id'])); 
                $branch_name = $branch->getList('name',array('branch_id'=>$aOrder['branch_id']));
                $num = $original->getList('nums',array('iostock_bn'=>$aOrder['iostock_bn']));
                $delivery = &app::get('ome')->model('delivery');
                $delivery_id = $delivery->getList('delivery_id',array('delivery_bn'=>$original_bn[0]['original_bn']));
                $delivery_order = &app::get('ome')->model('delivery_order');
                $delivery_order = $delivery_order->getList('order_id',array('delivery_id'=>$delivery_id[0]['delivery_id']));
                $order = &app::get('ome')->model('orders');
                $order_bn = $order->getList('order_bn',array('order_id'=>$delivery_order[0]['order_id']));
                if($aOrder['pay_status'] == '0'){
                   $pay_status = "未支付";
                }else{
                   $pay_status  = "已支付";
                }
                if($aOrder['sale_time']){
                    $time = date("Y-m-d/H:i:s",$aOrder['sale_time']);
				}else{
                    $time = '';
                }
                $aOrderRow = array();
                $aOrderRow['*:销售单号']     = $aOrder['sale_bn'];
                $aOrderRow['*:店铺名称']     = $shopid[0]['name'];
				$aOrderRow['*:店铺编码']     = $shopbn[0]['shop_bn'];
		        $aOrderRow['*:发货单号']	 = $original_bn[0]['original_bn'];
                $aOrderRow['*:出入库单号']	 = $aOrder['iostock_bn'];
	    		$aOrderRow['*:用户名']		 = $member_uname[0]['uname'];
				$aOrderRow['*:仓库编号']     = $branch_bn[0]['branch_bn'];
                $aOrderRow['*:仓库名称']	 = $branch_name[0]['name'];
             	$aOrderRow['*:销售金额']	 = $aOrder['sale_amount'];
				$aOrderRow['*:销售数量']	 = $num[0]['nums'];
                $aOrderRow['*:预收物流费']	     = $aOrder['delivery_cost'];
                $aOrderRow['*:预估物流费']	     = $aOrder['delivery_cost_actual'];
                $aOrderRow['*:附加费']	     = $aOrder['additional_costs'];
				$aOrderRow['*:订单折扣金额'] = $aOrder['discount'];
                $aOrderRow['*:预存款']	     = $aOrder['deposit'];
                $aOrderRow['*:备注']		 = $aOrder['memo'];
                $aOrderRow['*:支付状态']	 = $pay_status;
                $aOrderRow['*:销售时间']	 = $time;
                $aOrderRow['*:导购']	 = $aOrder['shopping_guide'];
                $aOrderRow['*:物流公司']	 = $aOrder['logi_name'];
                $aOrderRow['*:物流单号']	 = $aOrder['logi_no'];
			    $data['content']['sales'][]  = $this->charset->utf2local('"'.implode( '","', $aOrderRow ).'"');
                $objects = $this->db->select("SELECT * FROM sdb_ome_sales_items where sale_id=".$aOrder['sale_id']);//获取子数据
				if ($objects){	 
					 foreach ($objects as $obj){
                         $sql = 'SELECT name FROM sdb_ome_products WHERE bn like \''.$obj['bn'].'\'';
                         $name = kernel::database()->select($sql);	
                         $money = ($obj['price']*$obj['nums']);
                         $orderObjRow = array(); 
                         $orderObjRow['*:销售单号']   = $aOrder['sale_bn'];
						 $orderObjRow['*:订单号']	  = $order_bn[0]['order_bn'];
						 $orderObjRow['*:货号']       = $obj['bn'];
						 $orderObjRow['*:货品名称']   = $name[0]['name'];
						 $orderObjRow['*:销售单价']   = $obj['price'];
						 $orderObjRow['*:销售数量']   = $obj['nums'];
						 $orderObjRow['*:销售金额']   = $money;
						 $orderObjRow['*:导购']   = $aOrder['shopping_guide'];
						 $orderObjRow['*:店铺名称']     = $shopid[0]['name'];
						 $orderObjRow['*:店铺编码']     = $shopbn[0]['shop_bn'];
						 $orderObjRow['*:仓库编号']     = $branch_bn[0]['branch_bn'];
                		 $orderObjRow['*:仓库名称']	 = $branch_name[0]['name'];
                		 $orderObjRow['*:用户名']		 = $member_uname[0]['uname'];
                		 $orderObjRow['*:销售时间']	 = $time;
						 $data['content']['sales_items'][] = $this->charset->utf2local('"'.implode( '","', $orderObjRow ).'"');  
                     }
          
                }
            
           
        }
        $data['name'] = 'sales'.date("YmdHis");
        return true;
    }

    function export_csv($data,$exportType = 1 ){
        $output = array();
         foreach( $data['title'] as $k => $val ){
                $output[] = $val."\n".implode("\n",(array)$data['content'][$k]);
            }
     
        echo implode("\n",$output);
    }

 

}

?>